package com.hexu.event.view;

import com.hexu.event.entity.Attractions;
import com.hexu.event.entity.Memcon;
import com.hexu.event.entity.TicketHistory;
import com.hexu.event.bean.AttractionsBean;
import com.hexu.event.util.StageUtil;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.text.ParseException;
import java.util.*;

import static com.hexu.event.util.ServiceUtil.*;
import static com.hexu.event.util.ServiceUtil.ticketHistoryService;
import static com.hexu.event.view.UserView.*;
import static com.hexu.event.view.VisitorsView.*;


public class AdministratorView {

    //传入景点信息
    static List<Attractions> attractionsInformationList = attractionsService.getAttractionsInformation();

    static int attractionsNumber = attractionsService.attractionsNumber();

    static String selectName = null;
    static String selectOpenTime = null;
    static Integer selectTicketFare = null;
    static Integer selectTicketNumber = null;
    static String selectIntroduce = null;
    static Integer selectCommentNumber = null;
    public static Integer selectIndex = null;

    public static void view(Stage stage,int visitorsOrUsersOrAdministrator,String phoneNumber,int count,List<Attractions> list) {
        List<AttractionsBean> attractionsBeanList = new ArrayList<>();
        for(int i = 0;i<count;i++){
            //添加景点信息
            attractionsBeanList.add(  new AttractionsBean(list.get(i).getName(),list.get(i).getOpenTime(),
                    list.get(i).getTicketFare(),list.get(i).getTicketNumber(),
                    list.get(i).getIntroduce(),list.get(i).getCommentNumber())
            );
        }

        TableView<AttractionsBean> table = new TableView<>();

        //转化成表可存放的数据
        ObservableList<AttractionsBean> data =
                FXCollections.observableArrayList(
                        attractionsBeanList
                );

        //景点名字
        //列的数据类型
        TableColumn<AttractionsBean,String> nameCol =
                new TableColumn<>("景点名字");
        //设置出是最小宽度
        nameCol.setMinWidth(100);
        nameCol.setCellValueFactory(
                new PropertyValueFactory<>("name"));
        //开放时间
        TableColumn<AttractionsBean, String> openTimeCol =
                new TableColumn<>("开放时间");
        openTimeCol.setMinWidth(200);
        openTimeCol.setCellValueFactory(
                new PropertyValueFactory<>("openTime"));
        //门票价格
        TableColumn<AttractionsBean, Integer> ticketFareCol = new TableColumn<>("门票价格");
        ticketFareCol.setMinWidth(75);
        ticketFareCol.setCellValueFactory(
                new PropertyValueFactory<>("ticketFare"));
        //门票数量
        TableColumn<AttractionsBean, Integer> ticketNumberCol = new TableColumn<>("门票数量");
        ticketNumberCol.setMinWidth(75);
        ticketNumberCol.setCellValueFactory(
                new PropertyValueFactory<>("ticketNumber"));

        //介绍
        TableColumn<AttractionsBean, String> introduceCol =
                new TableColumn<>("景点介绍");
        introduceCol.setMinWidth(600);
        introduceCol.setMaxWidth(introduceCol.getPrefWidth());
        introduceCol.setCellValueFactory(
                new PropertyValueFactory<>("introduce"));

        //评论
        TableColumn<AttractionsBean, Integer> commentNumberCol = new TableColumn<>("评论数量");
        commentNumberCol.setMinWidth(75);
        commentNumberCol.setCellValueFactory(
                new PropertyValueFactory<>("commentNumber"));

        //接收信息
        table.setItems(data);
        //把数据放在相应列里
        table.getColumns().addAll(nameCol, openTimeCol, ticketFareCol,ticketNumberCol,introduceCol,commentNumberCol);


        //设置点击事件
        table.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                    //获取选择景点的序列
                    selectIndex = table.getSelectionModel().getSelectedIndex();
                    //如果点击的时候不是点击空处
                    if(selectIndex != -1){
                        //获取选择景点的信息
                        selectName = table.getSelectionModel().getSelectedItems().get(0).getName();
                        selectOpenTime = table.getSelectionModel().getSelectedItems().get(0).getOpenTime();
                        selectTicketFare = table.getSelectionModel().getSelectedItems().get(0).getTicketFare();
                        selectTicketNumber = table.getSelectionModel().getSelectedItems().get(0).getTicketNumber();
                        selectIntroduce = table.getSelectionModel().getSelectedItems().get(0).getIntroduce();
                        selectCommentNumber = table.getSelectionModel().getSelectedItems().get(0).getCommentNumber();

                    }
            }
        });

        Button add = new Button("添加景点");
        Button update = new Button("修改景点");
        Button delete = new Button("删除景点");
        Button viewUserInformation = new Button("查看用户信息");
        Button viewUserBuyHistory = new Button("查看用户购票历史");
        Button searchKey = new Button("关键词搜索");
        Button buyTicket = new Button("购票");
        Button viewComment = new Button("查看评论");
        Button returnLoginView = new Button("返回登录界面");
        Button back = new Button("返回");
        Button comment = new Button("评论");
        Button suggestionsOrComplaints = new Button("建议或申诉");
        Button refresh = new Button("刷新");
        Label searchDate = new Label("日期搜索：");

        HBox hbox = new HBox();
        Group group = new Group();

        hbox.setSpacing(6);

        //当身份不同时功能也会不同   ----- 管理员
        if(visitorsOrUsersOrAdministrator == 2){
            hbox.getChildren().addAll(add,update,delete,viewUserInformation,viewUserBuyHistory,searchKey,buyTicket,
                    viewComment,suggestionsOrComplaints,returnLoginView,refresh);

        }

        //----用户
        if(visitorsOrUsersOrAdministrator == 1){
            hbox.getChildren().addAll(searchKey,buyTicket,viewComment,comment,suggestionsOrComplaints,back,refresh);

        }
        //---游客
        if(visitorsOrUsersOrAdministrator == 0){
            hbox.getChildren().addAll(searchKey,viewComment,returnLoginView,refresh);

        }

        HBox hbox2 = new HBox();
        hbox2.setSpacing(6);
        ComboBox[] arr = createMonthAndDay();
        hbox2.getChildren().addAll(searchDate,createYear(),arr[0],arr[1],buttonView(visitorsOrUsersOrAdministrator,phoneNumber,stage));

        VBox vbox = new VBox();
        vbox.setSpacing(10);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(table, hbox,hbox2);

        group.getChildren().add(vbox);

        StageUtil.showStage(stage,new Scene(group),550,1200,"景点信息",false);

        //设置 添加景点 的单击事件
        add.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //调用工具类的管理员景点模板
                try {
                    Attractions attractions = new Attractions(null,null,"null",0,0,null,0);
                    StageUtil.showAdministratorAttractionsTemplate(table,data,"添加",attractions);
                    //更新信息
                    attractionsInformationList = attractionsService.getAttractionsInformation();
                    attractionsNumber = attractionsService.attractionsNumber();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


        //设置 修改景点 的单击事件
        update.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                if(selectIndex != null && selectIndex != -1){
                    Attractions attractions = new Attractions(null,selectName,selectOpenTime,selectTicketFare,
                            selectTicketNumber,selectIntroduce,selectCommentNumber);
                    //调用工具类显示管理员修改面版
                    try {
                        StageUtil.showAdministratorAttractionsTemplate(table,data,"修改",attractions);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        //设置 删除景点 单击事件
        delete.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                    if(selectIndex != null && selectIndex != -1){
                        administratorService.deleteAttractions(selectName);
                        int index = selectIndex;
                        table.getItems().remove(index);
                        //调用工具类方法显示成功或失败的消息
                        StageUtil.display(true,new Stage(),"删除");
                    }

                }

        });

        //设置 查看用户信息 单击事件
        viewUserInformation.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try {
                    userTableView.viewUserInformation();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        });

        //设置 查看用户购票历史 单击事件
        viewUserBuyHistory.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                List<TicketHistory> ticketHistoryList = ticketHistoryService.getAllUserTicketHistory();
                int ticketHistoryNumber = ticketHistoryService.allUserTicketNumber();
                ticketHistoryTableView.viewTicketHistoryInformation(ticketHistoryNumber,ticketHistoryList);
            }
        });


        //设置 关键词搜索 单击事件
        searchKey.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                searchKeyView.searchKeyWord (visitorsOrUsersOrAdministrator,phoneNumber,stage);
            }
        });

        //设置 购票 单击事件
        buyTicket.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(selectIndex != null && selectIndex != -1){
                   buy(phoneNumber,selectName,table);
                }

            }
        });

        //设置 返回登录界面 单击事件
        returnLoginView.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
               returnView(stage);
            }
        });

        //设置 返回 单击事件
        back.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.close();
                try {
                    userView.userView(stage);
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });

        //设置 评论 单击事件
        comment.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                commentView(table);

            }
        });

        //设置 查看评论 单击事件
        viewComment.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(selectIndex != null && selectIndex != -1){
                    CommentTableView.viewCommentContent(commentService.commentsNumber(selectName),commentService.getComment(selectName));
                }

            }
        });

        //设置 建议或申诉 单击事件
        suggestionsOrComplaints.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //用户
                if(visitorsOrUsersOrAdministrator == 1){
                    List<Memcon> list = memconService.getUserMemcon(phoneNumber);
                    int count =  memconService.memconUserNumber(phoneNumber);

                    MemconTableView.memconView(visitorsOrUsersOrAdministrator,phoneNumber,
                           count,list);
                }
                //管理员
                if(visitorsOrUsersOrAdministrator == 2){
                    List<Memcon> list =  memconService.getMenconInformation();
                    int count = memconService.memconNumber();
                    MemconTableView.memconView(visitorsOrUsersOrAdministrator,phoneNumber,
                           count ,list);
                }

            }
        });

        //设置 刷新 的单击事件
        refresh.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                stage.close();
                //取出景点的所有信息
                List<Attractions> listAttractionsAllInformation = attractionsService.getAttractionsInformation();
                //取出景点数量
                int attractionsCount = attractionsService.attractionsNumber();
                view(stage,visitorsOrUsersOrAdministrator,phoneNumber,attractionsCount, listAttractionsAllInformation);
            }
        });
    }



    public static void returnView(Stage stage){
        //关闭这个界面，打开登录界面
        stage.close();
        try {
            loginInView.start(new Stage());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //用户评论
    public static void commentView(TableView table){

        Label labelComment = new Label("评论");
        TextField textFieldComment = new TextField();
        Button sureComment = new Button("确定");

        labelComment.setLayoutY(20);
        textFieldComment.setLayoutY(20);
        sureComment.setLayoutY(20);
        labelComment.setLayoutX(20);
        textFieldComment.setLayoutX(80);
        sureComment.setLayoutX(250);
        Group group = new Group();
        group.getChildren().addAll(labelComment,textFieldComment,sureComment);

        Stage stage = new Stage();
        StageUtil.showStage(stage,new Scene(group),100,350,"评论",false);

        //设置 确定 的单击事件
        sureComment.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(textFieldComment != null && selectIndex != null && selectIndex != -1){
                    commentSure(textFieldComment.getText(),selectName);
                    //修改显示的数据
                    table.getItems().set(selectIndex,new AttractionsBean(selectName,selectOpenTime
                            ,selectTicketFare,selectTicketNumber,selectIntroduce,selectCommentNumber+1));
                    stage.close();
                }
            }
        });

    }
}
